import { Card, Form, Checkbox, Button, Input, message } from 'antd'
import logo from '@/assets/logo.png'
import './index.scss'
import { useStore } from '@/store'
import { useNavigate } from 'react-router-dom'



function Login () {
  const { loginStore } = useStore()
  const navigate = useNavigate()
  async function onFinish (values) {
    console.log(values)
    try {
      await loginStore.login({
        mobile: values.username,
        code: values.password
      })
      navigate('/', { replace: false })
      message.success('登录成功')
    } catch {
      message.error("登录失败")
    }

  }
  function onFinishFailed (values) {
  }
  return (
    <div className="login">
      <Card className='login-container'>
        <img className='login-logo' src={logo} alt=""></img>
        <Form initialValues={{ remember: true }} validateTrigger={['onBlur', 'onChange']} onFinish={onFinish} onFinishFailed={onFinishFailed}>
          <Form.Item name="username" rules={[{ required: true, message: '请输入手机号' }, {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号码格式不对',
            validateTrigger: 'onBlur'
          }]}>
            <Input size="large" placeholder="请输入手机号" />
          </Form.Item>
          <Form.Item name="password" rules={[{ required: true, message: '请输入密码' }]}>
            <Input size="large" placeholder="请输入密码" />
          </Form.Item>
          <Form.Item name='remember' valuePropName='checked'>
            <Checkbox className="login-checkbox-label">
              我已阅读并同意「用户协议」和「隐私条款」
            </Checkbox>
          </Form.Item>
          <Form.Item>
            <Button type="primary" htmlType="submit" size="large" block>
              登录
            </Button>
          </Form.Item>
        </Form>
      </Card>
    </div>
  )
}
export default Login